﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.DeepProcess;
using Server.API.DTOs;

namespace Server.API.Application.Handler.DeepProcess
{
    public class UpdateDeepProcessHandler:IRequestHandler<UpdateDeepProcessCommand, APIResult<int>>
    {
        private readonly IRepository<DeepProcessModel> deepProcessway;
        private readonly IMapper mapper;
        private readonly ILogger<GetDeepProcessHandler> log;

        public UpdateDeepProcessHandler(IRepository<DeepProcessModel> deepProcessway, IMapper mapper, ILogger<GetDeepProcessHandler> log)
        {
            this.deepProcessway = deepProcessway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 深加工修改
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        public async Task<APIResult<int>> Handle(UpdateDeepProcessCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            var value = mapper.Map<DeepProcessModel>(request);
            var result = await deepProcessway.Updateway(value);
            if(result > 0)
            {
                 log.LogInformation("深加工修改成功");
                res.Num = StateCode.OK;
                res.Message = "修改成功";

            }
            else
            {
                 log.LogError("深加工修改失败");
                res.Num = StateCode.Fail;
                res.Message = "修改失败";
            }
            return res;
             
        }
    }
}
